//
//  CalculateLoanTableView.m
//  YiJuZhangShou
//
//  Created by URANUS on 16/3/18.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "CalculateLoanTableView.h"
#import "LoadTableUtilView.h"
#import "Masonry.h"
#import "CalculateLoanTableViewModel.h"
@interface CalculateLoanTableView()<UITableViewDelegate,UIScrollViewDelegate>
@property (nonatomic, strong)LoadTableUtilView *tableBackView;
@property (nonatomic, strong)UITableView *tabelView;
@end

@implementation CalculateLoanTableView

#pragma mark Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadCalculateLoanTableView];
    }
    return self;
}

#pragma mark Delegate UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSArray *visibleCells = [_tabelView visibleCells];
    
    UITableViewCell *firstCell = visibleCells[0];
    NSIndexPath *indexPath = [_tabelView indexPathForCell:firstCell];
    DDLogInfo(@"位置index:%ld",(long)indexPath.row);

    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(scrollLoanTableToIndex:)]) {
        
        if (indexPath != nil) {
           [self.delegate scrollLoanTableToIndex:indexPath.row*2-1];
        }
    }
}

#pragma mark Event Response
- (void)titleTap:(UITapGestureRecognizer *)gesture{
    
    for (int i=1; i<6; i++) {
        UILabel *titelLabel = [self viewWithTag:i];
        titelLabel.textColor = [UIColor blackColor];
    }
    
    UILabel *selectTitleLabel = (UILabel *)gesture.view;
    [self changeChartsDataWithSelectedLabel:selectTitleLabel];
}

#pragma mark Private
- (void)loadCalculateLoanTableView{
    UIView *topBackView = [UIView new];
    _tableBackView = [LoadTableUtilView new];
    [self addSubview:topBackView];
    [self addSubview:_tableBackView];
    
    _tabelView = [_tableBackView loadStandardTableView];
    _tabelView.delegate = self;
    _tabelView.backgroundColor = BackgroundColor240;
    [topBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(44);
    }];
    [_tableBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBackView.mas_bottom);
        make.left.right.bottom.equalTo(self);
    }];
    
    [self loadTopView:topBackView];

}

- (void)changeChartsDataWithSelectedLabel:(UILabel *)selectLabel{
    selectLabel.textColor = [UIColor orangeColor];
    NSMutableArray *dataArray = [NSMutableArray array];
    
    for (CalculateLoanTableViewModel *model in _loanTableViewArray) {
        
        switch (selectLabel.tag) {
            case 1:
            {
                [dataArray addObject:model.monthlyRepayment];
            }
                break;
            case 2:
            {
                [dataArray addObject:model.monthlyRepaymentPrincipal];
            }
                break;
            case 3:
            {
                [dataArray addObject:model.monthlyRepaymentInterest];
            }
                break;
            case 4:
            {
                [dataArray addObject:model.leftPrincipal];
            }
                break;
            case 5:
            {
                [dataArray addObject:model.advanceRepayment];
            }
                break;
            default:
                break;
        }
    }
    
    if (self.delegate!=nil && [self.delegate respondsToSelector:@selector(setChartsDataArray:andTitle:)]) {
        [self.delegate setChartsDataArray:dataArray andTitle:selectLabel.text];
    }

}

- (void)loadTopView:(UIView *)backView {
    NSArray *titleArray = @[@"期数",@"月还款",@"月还本金",@"月还利息",@"剩余本金",@"提前还贷"];
    UILabel *lastLabel = nil;
    
    for (int i=0; i<6; i++) {
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.tag = i;
        
        if (i!=0) {
            titleLabel.userInteractionEnabled = YES;
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleTap:)];
            [titleLabel addGestureRecognizer:tapGesture];
        }
        
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
        titleLabel.text = titleArray[i];
        titleLabel.textColor = TextColor80;
        [backView addSubview:titleLabel];
        if (lastLabel) {
            
            if (i!=5) {
                
                [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastLabel.mas_right);
                    make.top.bottom.equalTo(lastLabel);
                    if (i==1) {
                       make.width.equalTo(backView).multipliedBy(0.15);
                    }else if (i == 2||i==3){
                       make.width.equalTo(backView).multipliedBy(0.17);
                    }
                }];
            }else{
                
                [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastLabel.mas_right);
                    make.top.bottom.equalTo(lastLabel);
                    make.right.equalTo(backView);
                    make.width.equalTo(lastLabel);
                    
                }];
            }
            
        }else{
            
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(backView);
                make.width.equalTo(backView).multipliedBy(0.11);
                make.top.bottom.equalTo(backView);
            }];
            
        }
        
        lastLabel = titleLabel;
    }
}

#pragma mark Setter
-(void)setLoanTableViewArray:(NSMutableArray *)loanTableViewArray{
    _loanTableViewArray = loanTableViewArray;
    [_tableBackView setModels:loanTableViewArray];
    UILabel *titleLabel  = [self viewWithTag:1];
    [self changeChartsDataWithSelectedLabel:titleLabel];
}


@end
